﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DataAccessLayer;
using GlobalObjects;
using System.Collections.Generic;

namespace DataAccessLayerTests
{
    [TestClass]
    public class SensorEntityTests
    {
        [TestMethod]
        public void addSensors_DAL_valid()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            Sensor sensor = new Sensor();
            sensor.Deviceid = 2113;
            sensor.Name = "pejmanSensor2";
            sensor.Type = "weather";
            sensor.Make = "GHI";
            sensor.Interval_measure = "3";
            sensor.Interval_send = "40";
            sensor.Metadata = "temp,humadity,time,location";
            //act
            Sensor newSensor = SensorEntity.addSensors_DAL(sensor);
            //assert
            Assert.IsNotNull(newSensor.Id);
            Assert.AreEqual(newSensor.Deviceid, 2113);
            Assert.AreEqual(newSensor.Name, "pejmanSensor2");
            Assert.AreEqual(newSensor.Type, "weather");
            Assert.AreEqual(newSensor.Make, "GHI");
            Assert.AreEqual(newSensor.Interval_measure, "3");
            Assert.AreEqual(newSensor.Interval_send, "40");

        }
        [TestMethod]
        [ExpectedException(typeof(System.Data.Entity.Infrastructure.DbUpdateException))]
        public void addSensors_DAL_invalidDeviceId()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            Sensor sensor = new Sensor();
            sensor.Deviceid = 144;
            sensor.Name = "gadgeteer";
            sensor.Type = "weather";
            sensor.Make = "GHI";
            sensor.Interval_measure = "3";
            sensor.Interval_send = "40";
            sensor.Metadata = "temp,humadity,time,location";
            //act
            Sensor newSensor = SensorEntity.addSensors_DAL(sensor);
            //assert
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void addSensors_DAL_invalidName()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            Sensor sensor = new Sensor();
            sensor.Deviceid = 1061;
            sensor.Name = "MyTempSensor2";
            //act
            Sensor newSensor = SensorEntity.addSensors_DAL(sensor);
            //assert
        }

        [TestMethod]
        public void getSensorsByDeviceId_DAL_validDeviceId()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            //act
            List<Sensor> list = SensorEntity.getSensorsByDeviceId_DAL(1057);
            //assert
            Assert.AreEqual(list.Count, 2);
        }
        [TestMethod]
        public void getSensorsByDeviceId_DAL_invalidDeviceId()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            //act
            List<Sensor> list = SensorEntity.getSensorsByDeviceId_DAL(114);
            //assert
            Assert.AreEqual(list.Count, 0);
        }
        [TestMethod]
        public void getTypeById_DAL_validId()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            //act
            String result = SensorEntity.getTypeById_DAL(1326);
            //assert
            Assert.AreEqual(result, "temperature");
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void getTypeById_DAL_invalidId()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            //act
            String result = SensorEntity.getTypeById_DAL(41);
            //assert
        }

        [TestMethod]
        public void getSensorById_DAL_valid()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            Sensor sensor = new Sensor();
            int sensorId = 1273;
            //act
            Sensor newSensor = SensorEntity.getSensorById_DAL(sensorId);
            //assert
            Assert.AreEqual(newSensor.Id, 1273);
            Assert.AreEqual(newSensor.Deviceid, 1061);
            Assert.AreEqual(newSensor.Name, "MyTempSensor1");
            Assert.AreEqual(newSensor.Type, "temperature");
            Assert.AreEqual(newSensor.Make, "GHI");
            Assert.AreEqual(newSensor.Interval_measure, "2000");
            Assert.AreEqual(newSensor.Interval_send, "6000");

        }
        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void getSensorById_DAL_invalid()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            //act
            Sensor result = SensorEntity.getSensorById_DAL(41);
            //assert
        }

        [TestMethod]
        public void setSensorDetails_DAL_valid()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            Sensor newSensor = new Sensor();
            newSensor.Id = 2318;
            newSensor.Type = "temperature";
            //act
            Sensor result = SensorEntity.updateSensorDetails_DAL(newSensor);
            //assert
            Assert.AreEqual(result.Id, 2318);
            Assert.AreEqual(result.Type, "temperature");
        }
        [TestMethod]
        public void setSensorDetails_DAL_valid2()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            Sensor newSensor = new Sensor();
            newSensor.Id = 1273;
            //act
            Sensor result = SensorEntity.updateSensorDetails_DAL(newSensor);
            //assert
            int deviceId = result.Deviceid;
            Assert.AreEqual(result.Id, 1273);
            Assert.AreEqual(result.Deviceid, 1061);
            Assert.AreEqual(result.Name, "MyTempSensor1");
            Assert.AreEqual(result.Type, "temperature");
            Assert.AreEqual(result.Make, "GHI");
            Assert.AreEqual(result.Interval_measure, "2000");
            Assert.AreEqual(result.Interval_send, "6000");
        }
        [TestMethod]
        [ExpectedException(typeof(System.Data.Entity.Infrastructure.DbUpdateException))]
        public void setSensorDetails_DAL_invalidDeviceId()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            Sensor newSensor = new Sensor();
            newSensor.Id = 1274;
            newSensor.Deviceid = 156;
            //act
            Sensor result = SensorEntity.updateSensorDetails_DAL(newSensor);
            //assert

        }
        [TestMethod]
        public void sensorNameExist_DAL_exist()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            Sensor newSensor = new Sensor();
            string name = "MyTempSensor2";
            //act
            Boolean result = SensorEntity.sensorNameExist_DAL(name);
            //assert
            Assert.IsTrue(result);
        }
        [TestMethod]
        public void sensorNameExist_DAL_notexist()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            Sensor newSensor = new Sensor();
            string name = "unknown";
            //act
            Boolean result = SensorEntity.sensorNameExist_DAL(name);
            //assert
            Assert.IsFalse(result);
        }
        [TestMethod]
        public void isAuthorised_DAL_authorised_Owner()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            Sensor newSensor = new Sensor();
            int sensorId = 1273;
            int userId = 1093;
            //act
            Boolean result = SensorEntity.isAuthorised_DAL(sensorId, userId);
            //assert
            Assert.IsTrue(result);
        }
        [TestMethod]
        public void isAuthorised_DAL_authorised_public()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            Sensor newSensor = new Sensor();
            int sensorId = 1274;
            int userId = 1090;
            //act
            Boolean result = SensorEntity.isAuthorised_DAL(sensorId, userId);
            //assert
            Assert.IsTrue(result);
        }
        [TestMethod]
        public void isAuthorised_DAL_authorised_shared()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            Sensor newSensor = new Sensor();
            int sensorId = 2317;
            int userId = 1092;
            //act
            Boolean result = SensorEntity.isAuthorised_DAL(sensorId, userId);
            //assert
            Assert.IsTrue(result);
        }
        [TestMethod]
        public void isAuthorised_DAL_unauthorised()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            Sensor newSensor = new Sensor();
            int sensorId = 2317;
            int userId = 1093;
            //act
            Boolean result = SensorEntity.isAuthorised_DAL(sensorId, userId);
            //assert
            Assert.IsFalse(result);
        }
        [TestMethod]
        public void setSensorStatus_DAL_valid()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            int id = 2357;
            Boolean status = true;
            //act
            SensorEntity.setSensorStatus_DAL(id, status);
            //assert
        }
        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void setSensorStatus_DAL_invalid()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            int id = 1073;
            Boolean status = false;
            //act
            SensorEntity.setSensorStatus_DAL(id, status);
            //assert
        }
        [TestMethod]
        public void getDistance_DAL_valid()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            int deviceId1 = 1057;
            int deviceId2 = 1060;
            //act
            double result = SensorEntity.getDistance_DAL(deviceId1, deviceId2);
            //assert
            Assert.AreEqual(714,Convert.ToInt32(result));
        }
        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void getDistance_DAL_invalid()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            int deviceId1 = 100;
            int deviceId2 = 1060;
            //act
            double result = SensorEntity.getDistance_DAL(deviceId1, deviceId2);
            //assert
        }

        [TestMethod]
        public void getNearestSensors_DAL_valid_existing() {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            int sensorId = 1273;
            //act
            List<SensorDistance> result = SensorEntity.getNearestSensors_DAL(sensorId);
            //assert
            Assert.AreEqual(result.Count, 2);
        }

        [TestMethod]
        public void getNearestSensors_DAL_valid_notExisting()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            int sensorId = 2317;
            double max = 10;
            //act
            List<SensorDistance> result = SensorEntity.getNearestSensors_DAL(sensorId);
            //assert
            Assert.AreEqual(result.Count, 0);
        }
        [TestMethod]
        public void addOrUpdateLimits_DAL_valid_both()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            int sensorId = 1276;
            Double upper = 32.2;
            Double lower = 12.2;
            //act
            SensorEntity.addOrUpdateLimits_DAL(sensorId, upper, lower, "temperature");
            //assert
            //Assert.AreEqual(result.Count, 0);
        }
        [TestMethod]
        public void addOrUpdateLimits_DAL_valid_one()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            int sensorId = 3372;
            Double upper = 29;
            //act
            SensorEntity.addOrUpdateLimits_DAL(sensorId, upper, null, "temperature");
            //assert
            //Assert.AreEqual(result.Count, 0);
        }
        [ExpectedException(typeof(System.Data.Entity.Infrastructure.DbUpdateException))]
        [TestMethod]
        public void addOrUpdateLimits_DAL_invalid_nullName()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            int sensorId = 1275;
            Double lower = 10.2;
            //act
            SensorEntity.addOrUpdateLimits_DAL(sensorId, null, lower, null);
            //assert
        }

        [TestMethod]
        public void getLimits_DAL_valid()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            int sensorId = 1274;
            //act
           Limits result = SensorEntity.getLimits_DAL(sensorId);
            //assert
            Assert.AreEqual(result.lower_limit, 10.2);
            Assert.AreEqual(result.upper_limit, null);
            Assert.AreEqual(result.trigger_name, "temperature");
        }

        [ExpectedException(typeof(System.Data.Entity.Infrastructure.DbUpdateException))]
        [TestMethod]
        public void getLimits_DAL_invalid()
        {
            //arrange
            SensorEntity SensorEntity = new SensorEntity();
            int sensorId = 12;
            //act
            Limits result = SensorEntity.getLimits_DAL(sensorId);
            //assert
        }

    }
}
